/**
 * XML Functional Dependence
 * ---------------------------
 * 
 * This class represents a functional dependence
 * XML with the transformation of path expressions in
 * Finite automata.
 * Thus we have:
 *
 * xdf = (M, (T'-> T"))
 *
 * where:
 * M and the context path;
 * T'and the source path or left side;
 * T"and the destination path or right side;
 * 
 * @author Eder P. C. Flor
 */
package model.constraints;

import java.util.Iterator;
import java.util.StringTokenizer;

import automata.Fsa;
import automata.State;

public class XDF extends AbstractConstraint {
	
	private static final long serialVersionUID = 1L;

	public XDF(String name, Fsa f1, Fsa f2, Fsa f3)
	{
		nameConstraint = name;
		contextFsa = f1;
		sourceFsa = f2;
		targetFsa = f3;
	}

	public XDF() {}
	
	/**
	 * Function that creates a path from the source FSA
	 * And there you have the token |
	 * @param s
	 * @return void, but set new source
	 */
	public void setSourceFsa(String s){
		leftPath = new String(s);
		sourceFsa = pathToFsa(s);
		//StringTokenizer st = new StringTokenizer(s, " ");
		//String token = new String();
		
		for (int i = 0; i < s.length(); i++)  
            if(s.charAt(i) == '|')  
            	npaths++;
        /*
		while (st.hasMoreTokens()){
			token = st.nextToken();
			
			if (token.equals("|"))
				npaths++;
		}*/
		setJ();
	}

	
	/**
	 * A patir de um FSA sabe qual tipo contextFsa, sourceFsa or targetFsa
	 * @param Object FSA
	 * @return integer 1 is contextFSA, 2 source FSA or 3 targetFSA
	 */
	public int getFsaPosition(Fsa f){
		//System.out.print("Define FsaPosition ");
		if (f == contextFsa){
			//System.out.println("contextFsa");
			return 1;
		}
			
		if (f == sourceFsa){
			//System.out.println("sourceFsa left");
			return 2;
		}
		if (f == targetFsa){
			//System.out.println("targetFsa right");
			return 3;	
		}			
		else
			return 0;
	}
	


	public void setNPaths(int n){
		npaths = n;
	}
	
	public void setJ(){
		Iterator<State> i = sourceFsa.get_final_states().iterator();
		
		while (i.hasNext()){
			j.add(i.next().elt);
		}
	}
	
	public int getJ(Integer state){
		return j.indexOf(state);
	}

	public String toStringPath() {
		String str = new String();
		str += "<b>XDF Name:</b> " + nameConstraint + "<br><br>";
		str += "<b>Constraint name:</b> " + context + "<br><br>";
		str += "<b>Left Path:</b> " + leftPath +  "<br><br>";
		str += "<b>Type of equality Left:</b> " + leftValue + "<br><br>";
		str += "<b>Right Path:</b> " + rightPath +  "<br><br>";
				
		if(rightValue == 0)
			str += "<b>Type of equality Right:</b> Value<br>";
		else
			str += "<b>Type of equality Right:</b> Node<br>";
		
		return str;
	}
	   
	public String toString() {
		String str = new String();
		str += "<b>XDF Name:</b> " + nameConstraint +  "<br><br>";
		str += "<b>Fsa Context:</b> " + contextFsa.toStringWeb() + "<br>";
		str += "<b>Fsa Source:</b> " + sourceFsa.toStringWeb() + "<br>";
		str += "<b>Fsa Target:</b> " + targetFsa.toStringWeb() + "<br>";
		str += "<b>Paths:</b> " + npaths + "<br>";
		return str;
	}
}
